
import config from '@/config'
import { getToken } from '@/utils/auth'
import { toast} from '@/utils/common'

let timeout = 10000
const baseUrl = config.baseUrl
let timer = null

const download = config => {
  // 是否需要设置 token
  const isToken = (config.headers || {}).isToken === false
  config.header = config.header || {}
  if (getToken() && !isToken) {
    config.header['Authorization'] = 'Bearer ' + getToken()
  }
	config.header['content-type'] = 'application/json'
	return new Promise((resolve, reject) => {
    uni.downloadFile({
        url: config.baseUrl || baseUrl + config.url,
        header: config.header,
      }).then(response => {
        let [error, res] = response
        if (error) {
          toast('后端接口连接异常')
          reject('后端接口连接异常')
          return
        }
        const code = res.statusCode  || 200
   
        resolve(res.tempFilePath)
      }) .catch(error => {
        let { message } = "下载失败"
        toast(message)
        reject(error)
      })
    
  })
}

export default download
